package com.tanxin.bill.modules;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tanxin.bill.R;
import com.tanxin.bill.common.base.BaseEvent;
import com.tanxin.bill.common.base.BaseTitleActivity;
import com.tanxin.bill.common.data.CustomerData;
import com.tanxin.bill.common.data.CustomerData_;
import com.tanxin.bill.common.weight.AddCustomerDialog;
import com.tanxin.bill.modules.adapter.CustomerAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.objectbox.Box;
import io.objectbox.android.AndroidScheduler;
import io.objectbox.query.Query;
import io.objectbox.reactive.DataObserver;

public class CustomerListActivity extends BaseTitleActivity {

    @BindView(R.id.recycle)
    RecyclerView recycle;
    private Box<CustomerData> customerBox;
    private Query<CustomerData> customerQuery;
    private CustomerAdapter customerAdapter;
    //0 跳转详情 1返回
    private int type;



    public static void startActivity(Context context,int type){
        Intent intent = new Intent(context,CustomerListActivity.class);
        intent.putExtra("type",type);
        context.startActivity(intent);
    }

    public static void startActivityForResut(Activity activity, int type,int code){
        Intent intent = new Intent(activity,CustomerListActivity.class);
        intent.putExtra("type",type);
        activity.startActivityForResult(intent,code);
    }

    @Override
    protected int bindLayout() {
        return R.layout.customer_list_activity;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        type = extras.getInt("type");
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        customerBox = MyApp.getApplication().getBoxStore().boxFor(CustomerData.class);

        customerAdapter = new CustomerAdapter(null);
        recycle.setLayoutManager(new LinearLayoutManager(getContext()));
        recycle.setAdapter(customerAdapter);
        customerAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                CustomerData data = (CustomerData) adapter.getData().get(position);
                if (type==0){
                    CustomerDetailActivity.startActivity(getContext(),data);
                }else {
                    Intent intent = new Intent();
                    intent.putExtra("data",data);
                    setResult(RESULT_OK,intent);
                    finish();
                }
            }
        });

        customerQuery = customerBox.query().order(CustomerData_.id).build();
        customerQuery.subscribe(subscriptions).on(AndroidScheduler.mainThread())
                    .observer(new DataObserver<List<CustomerData>>() {
                        @Override
                        public void onData(@NonNull List<CustomerData> data) {
                            customerAdapter.setNewData(data);
                        }
                    });
    }

    @Override
    protected void onEventComing(BaseEvent event) {

    }



    @Override
    protected String setTitle() {
        return "客户";
    }

    @OnClick(R.id.btn_add)
    public void onViewClicked() {
        new AddCustomerDialog(getContext(), new AddCustomerDialog.Customer() {
            @Override
            public void getInfo(final String name, final String phone) {
                CustomerData data = new CustomerData();
                data.setName(name);
                data.setPhone(phone);
                customerBox.put(data);
            }
        }).show(getSupportFragmentManager(), "add");
    }
}
